$.extend({
    getUrlVars: function () {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function (name) {
        return $.getUrlVars()[name];
    }
});
var userAgent = navigator.userAgent.toLowerCase();
$.browser = {
    version: (userAgent.match(/.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/) || [])[1],
    chrome: /chrome/.test(userAgent),
    safari: /webkit/.test(userAgent) && !/chrome/.test(userAgent),
    opera: /opera/.test(userAgent),
    msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
    mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
};


//(function ($) {
//    $.each(['show', 'hide'], function (i, ev) {
//        var el = $.fn[ev];
//        $.fn[ev] = function () {
//            this.trigger(ev);
//            return el.apply(this, arguments);
//        };
//    });
//})(jQuery);

function fixRadioButtonUnderRepeater(rb) {
  //Fix .net problem for radio button underneath repeater
  var rbs = $(".rptfix input:radio");
  for (var i = 0; i < rbs.length; i++) {
    if (rbs[i] != rb) rbs[i].checked = false;
  }
}

function validateAmount_4pt(txt) {
    var regex_4decimalPts = /^(?!0\d|$)\d*(\.\d{1,4})?$/
    var str = txt.val();

    return regex_4decimalPts.test(str);

}

function validateAmount_20pt(txt) {
    var regex_20decimalPts = /^(?!0\d|$)\d*(\.\d{1,20})?$/
    var str = txt.val();

    return regex_20decimalPts.test(str); ;

}

function validateAmount_60pt(txt) {
    var regex_20decimalPts = /^(?!0\d|$)\d*(\.\d{1,60})?$/
    var str = txt.val();

    return regex_20decimalPts.test(str); ;

}


function toggleCheckboxes(table, colnum, checked) {
    $("tr td:nth-child(" + colnum + ") input[type='checkbox']", table).prop("checked", checked);
}

function prepareFormInC1Console(form) {

    form = $(form);

    if (!form.isPrepared) {
        form.isPrepared = true;

        $("input, select, textarea", form).each(function () {

            var hTimeoutEnableNativeKeys = null;
            $(this).focus(function () {
                window.standardEventHandler.enableNativeKeys(true);
                hTimeoutEnableNativeKeys = window.setTimeout("window.standardEventHandler.enableNativeKeys(true);", 500);
            });

            $(this).blur(function () {
                clearTimeout(hTimeoutEnableNativeKeys);
                window.standardEventHandler.disableNativeKeys();
            });
        });
    }

}

function geC1ConsoletDockTabBinding() {

    if ($("page, ui\\:page").length == 0) {
        return null;
    }

    var frameWindow = (document.parentWindow || document.defaultView);
    var viewElem = $(frameWindow.frameElement).closest("ui\\:view");
    var viewBinding = eval("window.parent.bindingMap." + viewElem.attr("key"))
    var viewHandle = viewBinding.getHandle();

    var docktabElem = $("docktab[handle='" + viewHandle + "']", parent.document);
    var docktabBinding = eval("window.parent.bindingMap." + docktabElem.attr("key"));
    return docktabBinding;
}

function focusC1ConsolePageTreeNode(pageId) { 
    var tab = geC1ConsoletDockTabBinding();

    WebServiceProxy.isFaultHandler = false;
    var token = TreeService.GetEntityTokenByPageId(pageId);
    WebServiceProxy.isFaultHandler = true;

    if (token instanceof SOAPFault) {
    } else {
        if (token != null && token != "") {
            //window.setTimeout(function () {
                /*
                tab.getEntityToken = function () {
                    return token;
                }
                //tab.onActivate();
                tab._updateTree(true);
                tab.getEntityToken = DockTabBinding.prototype.getEntityToken;
                */
                var savedEntityToken = tab.getEntityToken();
                tab.setEntityToken(token);
                tab._entityToken = savedEntityToken;
            //}, 0);
        }
    }
}

function initTabTitleInC1Console() {

    if ($("page, ui\\:page").length == 0) {
        return;
    }

    var tabLabel = $("page:first, ui\\:page:first").attr("label");
    if (typeof (tabLabel) == "undefined" || tabLabel == "") {
        tabLabel = document.title;
    }
    if (typeof (tabLabel) == "undefined" || tabLabel == "") {
        tabLabel = $("h1:first").text();
    }

    if (typeof (tabLabel) != "undefined" && tabLabel != "") {
        var frameWindow = (document.parentWindow || document.defaultView);
        var viewElem = $(frameWindow.frameElement).closest("ui\\:view");
        var viewBinding = eval("window.parent.bindingMap." + viewElem.attr("key"))
        var viewHandle = viewBinding.getHandle();

        viewBinding.handleAction = function (args) {
            ViewBinding.prototype.handleAction.call(this, args);
            switch (args.type) {
                case PageBinding.ACTION_INITIALIZED:
                    window.setTimeout(function () {
                        try {
                            var docktabElem = $("docktab[handle='" + viewHandle + "']", parent.document);
                            var docktabBinding = eval("window.parent.bindingMap." + docktabElem.attr("key"));
                            docktabBinding.setToolTip(tabLabel);
                            docktabBinding.setLabel(tabLabel);
                        } catch (e) {
                        }
                    }, 0);
                    break;
            }
        };
    }

}


function enableInputControls(panel, enabled) {
    if (typeof (enabled) == "undefined")
        enabled = true;

    $("input, select, button, textarea", panel).add($(panel).filter("input, select, button, textarea")).prop("disabled", !enabled);
}

function disableInputControls(panel) {
    enableInputControls(panel, false);
}

function enableValidators(panel, enabled) {
    if (typeof (enabled) == "undefined")
        enabled = true;

    var validators = $("span[id*='_vld'].errormessage", panel).add($(panel).filter("span[id*='_vld'].errormessage"));
    validators.each(function () {
        try { eval(this.id + ".enabled = " + enabled + ";"); } catch (e) { }
    });
}

function disableValidators(panel) {
    enableValidators(panel, false);
}

$(document).ready(function () {
    $('form').each(function () {
        prepareFormInC1Console(this);
    });

    $("textarea").each(function () {
        $(this).val($.trim($(this).val()));
    });

    $(":text, :password, :file").keydown(function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            return false;
        }
    });

    $(".footer-container").prev(".content-container").css("margin-bottom", "50px");

    // if ($.browser.msie && $.browser.version == "7.0"
    if ($.browser.msie) {
        $("ui\\:page").css('visibility', 'visible');
    }
});

$(window).load(function () {

});
